<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/27
 * Time: 10:45
 */

namespace Admin\Controller;

use Think\Controller;

class KhzyController extends AdminController
{
    //对父类构造函数的补充
    public function _myinitialize()
    {
        $this->db = M('khzy');
        $this->session_lst = CONTROLLER_NAME."lst";
        $this->username=session('user_auth.username');
    }

    public function lst()
    {
        $session_where=I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        if(empty($session_where)){
            //查询条件
            $where['status'] = array('gt', 0);

            //所有客户名称列表
            $khmc_arr = $this->db->where($where)->getField('khmc', true);
            $khmc_arr = array_unique($khmc_arr);

            //所有对应内部公司列表
            $dynbgs_arr = $this->db->where($where)->getField('dynbgs', true);
            $dynbgs_arr = array_unique($dynbgs_arr);

            $month = I('month');
            $year = I('year');
            if (!empty($month) && !empty($year)) { //所属月份
                $ssyf = $year . $month;
                $where['yf'] = $ssyf;
            }

            $dynbgs = I('dynbgs');
            if (!empty($dynbgs)) {  //对应内部公司
                $where['dynbgs'] = I('dynbgs');
            }

            $khmc = I('khmc');
            if (!empty($khmc)) { //客户名称
                $where['khmc'] = I('khmc');
            }

            if (count($where) === 1) { //此时查询条件仅为status=1，表示没有别的查询条件，则默认查询条件为：只显示上月的数据
                $last_m = date("Ym",strtotime(date("Ym01",time()))-8600);;//默认情况下显示上月的数据
                $where['yf'] = $last_m;
            }
            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME."where",$where);
        }else{
            $where=session(CONTROLLER_NAME."where");
        }

        //列表数据
        $lsts = $this->db->where($where)->order("updatetime desc")->select();

        $this->assign('khmc_arr', $khmc_arr);//所有客户名称列表
        $this->assign('dynbgs_arr', $dynbgs_arr);//所有对应内部公司列表
        $this->assign('lsts', $lsts);//列表数据

        //将数据存入session中，方便导出
        session($this->session_lst, null);
        session($this->session_lst, $lsts);

        $this->display();
    }

    //新增 www.glxt.com/admin/khzy/add
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['sssj'] = date('Ymd', strtotime($data['sssj']));
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['ktrq'] = date('Ymd', strtotime($data['ktrq']));
                $data['jsrq'] = date('Ymd', strtotime($data['jsrq']));
                $data['jfrq'] = date('Ymd', strtotime($data['jfrq']));
                $data['rqqj'] = $data['jfrq'] . "-" . $data['jsrq'];
                $data['ts_sum'] = (strtotime($data['jsrq']) - strtotime($data['jfrq'])) / 86400+1;
                $data['updatetime'] = time();
                $data['status'] = 1;
                $t = date('t', strtotime($data['sssj']));//该月的总天数
                $data['je'] = $data['ts_sum'] / $t * $data['dj'] * $data['gs'];//金额=当月使用天数/当月实际天数*个数*单价
//                $data['je'] = floor($data['je'] * 100) / 100;
                $data['je']=round($data['je'],2);

                //itz:页面传来的调整值 tz:调整总金额(第一次新增时 itz=tz) tz_h:调整的历史记录 格式为：array("2017-05-27 11:39:10"=>-20,)
                $itz=I('itz');
                if(!empty($itz)){
                    $data['tz_h']=json_encode(array($this->username."_".date("Y-m-d H:i:s")=>$itz));
                    $data['tz']=$itz;
                }

                $data['ys'] = $data['je'] + $data['tz'];//应收=金额+调整
            }
            $rid = $this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //修改
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['sssj'] = date('Ymd', strtotime($data['sssj']));
                $data['yf'] = date('Ym', strtotime($data['sssj']));
                $data['ktrq'] = date('Ymd', strtotime($data['ktrq']));
                $data['jsrq'] = date('Ymd', strtotime($data['jsrq']));
                $data['jfrq'] = date('Ymd', strtotime($data['jfrq']));
                $data['rqqj'] = $data['jfrq'] . "-" . $data['jsrq'];
                $data['ts_sum'] = (strtotime($data['jsrq']) - strtotime($data['jfrq'])) / 86400+1;
                $data['updatetime'] = time();
                $data['status'] = 1;
                $t = date('t', strtotime($data['sssj']));//该月的总天数
                $data['je'] = $data['ts_sum'] / $t * $data['dj'] * $data['gs'];//金额=当月使用天数/当月实际天数*个数*单价
//                $data['je'] = floor($data['je'] * 100) / 100;
                $data['je']=round($data['je'],2);

                $itz=I('itz');
                $c_tz_h=$this->db->where(array("id"=>$data['id']))->getField('tz_h');
                $c_tz_h_arr=json_decode($c_tz_h,true);
                if(!empty($c_tz_h_arr)){ //表明之前tz_h里面已经有值
                    if(!empty($itz)){//把新值加入
                        $c_tz_h_arr[$this->username."_".date("Y-m-d H:i:s")]=$itz;
                        $data['tz']=array_sum($c_tz_h_arr);
                        $data['tz_h']=json_encode($c_tz_h_arr);
                    }else{//表明没有做单次调整，则tz还是用以前的值，此值也可以直接查出，这里重新计算了一下，调整历史不变不修改
                        $data['tz']=array_sum($c_tz_h_arr);
                    }
                }else{ //表明之前tz_h里面无值，是第一次调整
                    $data['tz_h']=json_encode(array($this->username."_".date("Y-m-d H:i:s")=>$itz));
                    $data['tz']=$itz;
                }

                $data['ys'] = $data['je'] + $data['tz'];//应收=金额+调整
            }
            $rid = $this->db->save($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst',array("session_where"=>"session_where"));
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            $this->ajaxReturn($data);
        }
    }

    //删除不是物理删除，只是状态的改变
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $this->db->where(array('id' => $id))->delete();
            $this->redirect('lst',array("session_where"=>"session_where"));
        }
    }

    //通过excel导入数据
    public function import()
    {
        if (IS_POST) {
            $this->import_excel();
        }
    }

    //根据表的不同来处理数据
    protected function handle_data($data){

        foreach ($data as $k => $v) {
            $data[$k]['sssj'] = date('Ymd', strtotime($data[$k]['sssj']));
            $data[$k]['yf'] = date('Ym', strtotime($data[$k]['sssj']));
            $data[$k]['ktrq'] = date('Ymd', strtotime($data[$k]['ktrq']));
            $data[$k]['jsrq'] = date('Ymd', strtotime($data[$k]['jsrq']));
            $data[$k]['jfrq'] = date('Ymd', strtotime($data[$k]['jfrq']));
            $data[$k]['rqqj'] = $data[$k]['jfrq'] . "-" . $data[$k]['jsrq'];
            $data[$k]['ts_sum'] = (strtotime($data[$k]['jsrq']) - strtotime($data[$k]['jfrq'])) / 86400+1;
            $data[$k]['updatetime'] = time();
            $data[$k]['status'] = 1;
            $t = date('t', strtotime($data[$k]['sssj']));//该月的总天数
            $data[$k]['je'] = $data[$k]['ts_sum'] / $t * $data[$k]['dj'] * $data[$k]['gs'];//金额=当月使用天数/当月实际天数*个数*单价
//            $data[$k]['je'] = floor($data[$k]['je'] * 100) / 100;
            $data[$k]['je'] =round($data[$k]['je'],2);

            //tz:页面传来的调整值 tz:调整总金额(第一次新增时 tz=tz) tz_h:调整的历史记录 格式为：array("2017-05-27 11:39:10"=>-20,)
            $tz=$data[$k]['tz'];
            if(!empty($tz)){
                $data[$k]['tz_h']=json_encode(array($this->username."_".date("Y-m-d H:i:s")=>$tz));
            }

            $data[$k]['ys'] = $data[$k]['je'] + $data[$k]['tz'];//应收=金额+调整
        }

        return $data;
    }

    //导出数据到csv
    public function csv_export(){
        $this->export();
    }
}